import Vue from 'vue'
import axios from 'axios'
Vue.prototype.$http = axios

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    let res = response.data
    let path = location.hash
    if (res.errno === 0) {
        // 正常返回
        return res
    } else if (res.errno === 10) {
        // 未登录
        if (path != '#/index') {
            // window.location.href = '/#/login'
            window.location.href = 'http://129.211.50.208:8001/dist/#/login'
        }
        return Promise.reject(res)
    } else if (res.errno === 100) {
        // 登录失败
        return res
    } else if (res.errno === 15) {
        // 验证码错误
        return res
    } else if (res.errno == 10010) {
        // 无权限成新登录
        // window.location.href = '/#/login'
        window.location.href = 'http://129.211.50.208:8001/dist/#/login'
    }
    else {
        alert(res.msg)
        return Promise.reject(res)
    }
}, err => {
    // alert(err.response.data.message)
    return Promise.reject(err)
});

// 添加请求拦截器
axios.interceptors.request.use(config => {
    let token = localStorage.getItem('token')
    // let uid = sessionStorage.getItem('uid')
    config.headers['Authorization'] = 'Bearer ' + token;
    // if (uid) {
    //     axios.defaults.headers.common["uid"] = uid;
    // }
    return config
})

//线上
// axios.defaults.baseURL='http://129.211.50.208:3000'  
axios.defaults.baseURL = 'http://localhost:3000'

//   跨域可以携带cookie
// axios.defaults.withCredentials = true;

export default axios